package com.zx.znav.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zx.znav.bean.Navigation;
import com.zx.znav.service.NavigationService;
import com.zx.znav.mapper.NavigationMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author lenovo
* @description 针对表【t_navigation】的数据库操作Service实现
* @createDate 2023-07-19 16:44:10
*/
@Service
public class NavigationServiceImpl extends ServiceImpl<NavigationMapper, Navigation>
    implements NavigationService{

    @Override
    public List<Navigation> listFirstNavigation() {
        LambdaQueryWrapper<Navigation> wrapper = new LambdaQueryWrapper<>();
        //parentId = 0 为一级导航
        wrapper.eq(Navigation::getParentId, 0);
        //按优先级倒序排
        wrapper.orderByDesc(Navigation::getPriority).orderByAsc(Navigation::getName);;
        return list(wrapper);
    }

    @Override
    public List<Navigation> listSecondNavigation() {
        LambdaQueryWrapper<Navigation> wrapper = new LambdaQueryWrapper<>();
        //parentId = 0 为一级导航
        wrapper.ne(Navigation::getParentId, 0);
        //按优先级倒序排
        wrapper.orderByDesc(Navigation::getPriority).orderByAsc(Navigation::getName);;
        return list(wrapper);
    }


    @Override
    public List<Navigation> searchFirstNavigation(Navigation navigation) {
        LambdaQueryWrapper<Navigation> wrapper = new LambdaQueryWrapper<>();
        //一级导航parentId = 0
        wrapper.eq(Navigation::getParentId, 0);
        //根据名称模糊查询
        wrapper.like(navigation.getName() != null, Navigation::getName, navigation.getName());
        wrapper.orderByDesc(Navigation::getPriority).orderByAsc(Navigation::getName);
        return list(wrapper);
    }

    @Override
    public List<Navigation> searchSecondNavigation(Navigation navigation) {
        LambdaQueryWrapper<Navigation> wrapper = new LambdaQueryWrapper<>();
        //根据名称模糊查询
        wrapper.like(navigation.getName() != null, Navigation::getName, navigation.getName());
        //二级菜单parentId > 0
        wrapper.gt(Navigation::getParentId, 0);
        wrapper.eq(navigation.getParentId() != null, Navigation::getParentId, navigation.getParentId());
        wrapper.orderByDesc(Navigation::getPriority).orderByAsc(Navigation::getName);
        return list(wrapper);
    }

    @Override
    public List<Navigation> listSecondNavByFirstNavId(Integer id) {
        LambdaQueryWrapper<Navigation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Navigation::getParentId, id);
        wrapper.orderByDesc(Navigation::getPriority).orderByDesc(Navigation::getLinkCnt).orderByAsc(Navigation::getName);
        return list(wrapper);
    }

    @Override
    public void operateLinkCntById(Integer id, boolean increase) {
        LambdaUpdateWrapper<Navigation> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Navigation::getId, id);
        String sql = "link_cnt = link_cnt ";
        if(increase){
            sql += "+ 1";
        }else{
            sql += "- 1";
        }
        wrapper.setSql(sql);
        update(wrapper);
    }

    @Override
    public Integer countSecondNavByFirstNavId(Integer firstNavId) {
        LambdaQueryWrapper<Navigation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Navigation::getParentId, firstNavId);
        return count(wrapper);
    }
}




